
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.cuotigou.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

public class HttpUtils {
    private static final int maxConnPerHost = 1000;
    private static final int maxTotalConn = 1000;
    private static final int connectionTimeout = 10000;
    private static final int soTimeout = 30000;
    private static final int connectionManagerTimeout = 5000;
    private static HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();

    static {
        HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
        connectionManagerParams.setConnectionTimeout(10000);
        connectionManagerParams.setSoTimeout(30000);
        connectionManagerParams.setMaxTotalConnections(1000);
        connectionManagerParams.setDefaultMaxConnectionsPerHost(1000);
        connectionManagerParams.setParameter("http.protocol.content-charset", "UTF-8");
        connectionManagerParams.setParameter("http.protocol.element-charset", "UTF-8");
        connectionManagerParams.setParameter("http.protocol.credential-charset", "UTF-8");
        httpConnectionManager.setParams(connectionManagerParams);
    }

    public HttpUtils() {
    }

    public static HttpClient getHttpClient() {
        HttpClient httpClient = new HttpClient(httpConnectionManager);
        httpClient.getParams().setConnectionManagerTimeout(5000L);
        return httpClient;
    }

    public static String executeGetMethod(String url, String resultEncoding) throws IOException, HttpException {
        return executeGetMethod((String)url, resultEncoding, (List)null);
    }

    public static String executeGetMethod(String url, List<Header> headerList) throws IOException, HttpException {
        return executeGetMethod((String)url, (String)null, (List)headerList);
    }

    public static String executeGetMethod(String url) throws IOException, HttpException {
        return executeGetMethod((String)url, (String)null, (List)null);
    }

    public static String executeGetMethod(String url, String resultEncoding, List<Header> headerList) throws IOException, HttpException {
        return executeGetMethod(getHttpClient(), url, resultEncoding, headerList);
    }

    public static byte[] executeGetMethodAsBytes(String url) throws IOException, HttpException {
        return executeGetMethodAsBytes(getHttpClient(), url);
    }

    public static byte[] executeGetMethodAsBytes(HttpClient client, String url) throws IOException, HttpException {
        GetMethod method = new GetMethod(url);

        byte[] var5;
        try {
            client.executeMethod(method);
            var5 = method.getResponseBody();
        } catch (HttpException var9) {
            throw var9;
        } catch (IOException var10) {
            throw var10;
        } finally {
            method.releaseConnection();
        }

        return var5;
    }

    public static String executeGetMethod(HttpClient client, String url, String resultEncoding) throws IOException, HttpException {
        return executeGetMethod(client, url, resultEncoding, (List)null);
    }

    public static String executeGetMethod(HttpClient client, String url, String resultEncoding, List<Header> headerList) throws IOException, HttpException {
        GetMethod method = new GetMethod(url);
        if(headerList != null) {
            Iterator var6 = headerList.iterator();

            while(var6.hasNext()) {
                Header header = (Header)var6.next();
                method.addRequestHeader(header);
            }
        }

        String var7;
        try {
            client.executeMethod(method);
            if(resultEncoding != null) {
                var7 = new String(method.getResponseBody(), resultEncoding);
                return var7;
            }

            var7 = method.getResponseBodyAsString();
        } catch (HttpException var11) {
            throw var11;
        } catch (IOException var12) {
            throw var12;
        } finally {
            method.releaseConnection();
        }

        return var7;
    }

    public static String[] executeGetMethodRetHeader(String url, String resultEncoding) throws IOException, HttpException {
        GetMethod method = new GetMethod(url);

        String[] var8;
        try {
            getHttpClient().executeMethod(method);
            String cookie = "";
            Header header = method.getResponseHeader("Set-Cookie");
            if(header != null) {
                cookie = header.getValue();
            }

            String[] result = new String[2];
            String content;
            if(resultEncoding != null) {
                content = new String(method.getResponseBody(), resultEncoding);
                result[0] = content;
            } else {
                content = method.getResponseBodyAsString();
                result[0] = content;
            }

            result[1] = cookie;
            var8 = result;
        } catch (HttpException var12) {
            throw var12;
        } catch (IOException var13) {
            throw var13;
        } finally {
            method.releaseConnection();
        }

        return var8;
    }

    public static String executePostMethod(String url, String encod, String xmlStr) throws IOException, HttpException {
        PostMethod method = new PostMethod(url);
        InputStream inputStream = new ByteArrayInputStream(xmlStr.getBytes());
        RequestEntity request = new InputStreamRequestEntity(inputStream, "application/xml; charset=" + encod);
        method.setRequestEntity(request);

        String var8;
        try {
            getHttpClient().executeMethod(method);
            if(encod != null) {
                var8 = new String(method.getResponseBody(), encod);
                return var8;
            }

            var8 = method.getResponseBodyAsString();
        } catch (HttpException var12) {
            throw var12;
        } catch (IOException var13) {
            throw var13;
        } finally {
            method.releaseConnection();
        }

        return var8;
    }

    public static String executePostJsonMethod(String url, String encod, String jsonStr) throws IOException, HttpException {
        PostMethod method = new PostMethod(url);
        InputStream inputStream = new ByteArrayInputStream(jsonStr.getBytes());
        RequestEntity request = new InputStreamRequestEntity(inputStream, "application/json; charset=" + encod);
        method.setRequestEntity(request);

        String var8;
        try {
            getHttpClient().executeMethod(method);
            if(encod == null) {
                var8 = method.getResponseBodyAsString();
                return var8;
            }

            var8 = new String(method.getResponseBody(), encod);
        } catch (HttpException var12) {
            throw var12;
        } catch (IOException var13) {
            throw var13;
        } finally {
            method.releaseConnection();
        }

        return var8;
    }

//    public static String executePostMethod(String url, String resultEncoding, Map<String, String> paramsMap) throws IOException, HttpException {
//        return executePostMethod(url, resultEncoding, (String)null, (Header[])null, paramsMap);
//    }

//    public static String executePostMethod(String url, String resultEncoding, String cookie, Header[] headers, Map<String, String> paramsMap) throws IOException, HttpException {
//        PostMethod method = new HttpUtils.UTF8PostMethod(url);
//        NameValuePair[] values = new NameValuePair[paramsMap.size()];
//        int i = 0;
//
//        for(Iterator var9 = paramsMap.entrySet().iterator(); var9.hasNext(); ++i) {
//            Entry<String, String> entry = (Entry)var9.next();
//            values[i] = new NameValuePair((String)entry.getKey(), (String)entry.getValue());
//        }
//
//        if(StringUtils.isNotBlank(cookie)) {
//            method.setRequestHeader("Cookie", cookie);
//        }
//
//        method.setRequestBody(values);
//        if(headers != null) {
//            Header[] var11 = headers;
//            int var10 = headers.length;
//
//            for(int var19 = 0; var19 < var10; ++var19) {
//                Header header = var11[var19];
//                method.addRequestHeader(header);
//            }
//        }
//
//        String var20;
//        try {
//            getHttpClient().executeMethod(method);
//            if(resultEncoding == null) {
//                var20 = method.getResponseBodyAsString();
//                return var20;
//            }
//
//            var20 = new String(method.getResponseBody(), resultEncoding);
//        } catch (HttpException var15) {
//            throw var15;
//        } catch (IOException var16) {
//            throw var16;
//        } finally {
//            method.releaseConnection();
//        }
//
//        return var20;
//    }

//    public static Map<String, String> parseContent(String url) {
//        Map<String, String> retMap = new HashMap();
//        String[] ary = org.apache.commons.lang.StringUtils.split(url, "&");
//
//        for(int i = 0; ary != null && i < ary.length; ++i) {
//            String[] keyValue = org.apache.commons.lang.StringUtils.split(ary[i], "=");
//            if(keyValue != null && keyValue.length > 0) {
//                if(keyValue.length > 1) {
//                    retMap.put(keyValue[0], keyValue[1]);
//                } else {
//                    retMap.put(keyValue[0], "");
//                }
//            }
//        }
//
//        return retMap;
//    }

    public static void main(String[] args) throws Exception {
    }

    private static class UTF8PostMethod extends PostMethod {
        public UTF8PostMethod(String url) {
            super(url);
        }

        public String getRequestCharSet() {
            return "UTF-8";
        }
    }
}
